from components.exts import db
from enum import Enum
from datetime import datetime

class FileStatusEnum(Enum):
    PROCESSED = '处理完成'
    UNPROCESSED = '已接收'
    PROCESSING = '处理中'
    ERR = '未知错误'


class Status(db.Model):
    __tablename__ = 'process_status'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    file_name = db.Column(db.String(255), nullable=False)
    file_uuid = db.Column(db.String(255), nullable=False)
    file_status = db.Column(db.Enum(FileStatusEnum), default=FileStatusEnum.UNPROCESSED)

    def change_status(self):
        s = Status.query.get(self.id)
        if s.file_status.value == '已接收':
            s.file_status = FileStatusEnum.PROCESSING
        elif s.file_status.value == '处理中':
            s.file_status = FileStatusEnum.PROCESSED
        db.session.commit()

    def status2err(self):
        s = Status.query.get(self.id)
        s.file_status = FileStatusEnum.ERR
        db.session.commit()


class UStatus(db.Model):
    __tablename__ = 'uie_status'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    file_name = db.Column(db.String(255), nullable=False)
    file_uuid = db.Column(db.String(255), nullable=False)
    file_status = db.Column(db.Enum(FileStatusEnum), default=FileStatusEnum.UNPROCESSED)

    def change_status(self):
        s = UStatus.query.get(self.id)
        if s.file_status.value == '已接收':
            s.file_status = FileStatusEnum.PROCESSING
        elif s.file_status.value == '处理中':
            s.file_status = FileStatusEnum.PROCESSED
        db.session.commit()

    def status2err(self):
        s = UStatus.query.get(self.id)
        s.file_status = FileStatusEnum.ERR
        db.session.commit()

class SensorData(db.Model):
    __tablename__ = 'sensor_data'

    id = db.Column(db.Integer, primary_key=True)
    datetime = db.Column(db.DateTime, default=datetime.utcnow)

    # 输入变量
    FIC1007 = db.Column(db.Float)
    FIC1001A = db.Column(db.Float)
    FIC1001B = db.Column(db.Float)
    FIC1001C = db.Column(db.Float)
    FIC1001D = db.Column(db.Float)
    FIC1002A = db.Column(db.Float)
    FIC1002B = db.Column(db.Float)
    FIC1002C = db.Column(db.Float)
    FIC1002D = db.Column(db.Float)
    TIC1001 = db.Column(db.Float)
    TIC1005 = db.Column(db.Float)
    TIC1008 = db.Column(db.Float)
    TIC2005 = db.Column(db.Float)

    # 输出变量
    FIC1008 = db.Column(db.Float)
    FI615 = db.Column(db.Float)

    def to_dict(self):
        return {col.name: getattr(self, col.name) for col in self.__table__.columns}